package com.bigfreakindeals.Controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;

import com.bigfreakindeals.Models.ObjectImage;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class ImageEngine implements Runnable{

	 private HttpClient httpClient;
	   
	List<ObjectImage> inQuery;
	boolean stop = false;
	ObjectImage imagetoSend = null;
	static ImageEngine instance;
	
	 public static ImageEngine getInstance() {
	        if (instance == null)
	            instance = new ImageEngine();
	        return instance;
	    }
	 
	 public ImageEngine() 
	    {
	       inQuery=new ArrayList<ObjectImage>();
	       new Thread(this).start();  
	    }
	private boolean isinQueue(String imgID){
		if (inQuery==null)
			return false;
		if (inQuery.size() == 0)
			return false;
		
		for (int i = 0; i < inQuery.size();i++){
			ObjectImage obj = (ObjectImage)inQuery.get(i);
			if (obj.getImgid().compareTo(imgID) == 0){
				return true;
					
		}
				}
		return false;
	}
	public synchronized void getImage(ObjectImage objimage)
	    {
			if(!stop)
	        {
	           synchronized(inQuery)
	           {   
	            	//check is deal id is already in the que;
	            	if (!isinQueue(objimage.getImgid())){
	            		inQuery.add(objimage);
	            		inQuery.notifyAll();
	            	}
	            }
	        }
	   }
	
	public void stop(){
		
	}
	public void downloadImage(){
		
		HttpResponse response = null;
    	
    	try {
        
        	httpClient = new DefaultHttpClient();
            
        	HttpConnectionParams.setSoTimeout(httpClient.getParams(), 15000);
            
        	response = httpClient.execute(new HttpGet(imagetoSend.getUrl()));
            
        	BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(response.getEntity());
            
        	Bitmap bm = BitmapFactory.decodeStream(bufHttpEntity.getContent());
            
        	
        		
        	 if (imagetoSend.getListener()!=null && bm!=null)
        		 (imagetoSend.getListener()).imageResponse(bm, imagetoSend.getImgid(),imagetoSend.getUniversityId());
            
    	} catch (Exception e) {
        	//TODO: send error message
        	 Log.i("exception","  "+e.getMessage()+" "+imagetoSend.getUrl()+" "+imagetoSend.getUniversityId()) ;     
                
             
        }
 
 }  
	
    public void run()
    {
        while(!stop || ! inQuery.isEmpty())
        {
            synchronized( inQuery )
            {
                while( !stop && inQuery.isEmpty() )
                {
                    try 
                    {
                        inQuery.wait();
                    }
                    catch( InterruptedException e )
                    
                    {
                         if(!(imagetoSend.getListener()==null))
                        	imagetoSend.getListener().imageResponse(null,null,null);
                        stop();
                        break;
                    }
                }

                if( !inQuery.isEmpty() )
                {
                	imagetoSend = (ObjectImage) inQuery.get( 0 );
                	downloadImage();
                    inQuery.remove( 0 );
                	inQuery.notifyAll();
                    
                } 
            }
        }    
    }
}
